package cn.wen.yinxiu.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wen.yinxiu.common.convention.result.Results;
import cn.wen.yinxiu.common.to.system.SearchHistoryTO;
import cn.wen.yinxiu.member.vo.SearchHistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yinxiu.member.entity.SearchHistoryEntity;
import cn.wen.yinxiu.member.service.SearchHistoryService;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.convention.result.ResultEntity;



/**
 * 
 *
 * @ClassName: SearchHistory
 * @Author 小飞
 * @Email coderwen22@163.com
 * @Date 2023-05-21 22:19:26
 * @Description:
 */
@RestController
@RequestMapping("/web")
public class SearchHistoryController {

    @Autowired
    private SearchHistoryService searchHistoryService;

    /**
     * 获取查询记录
     *
     * @param userId
     * @param browserId
     * @return
     */
    @GetMapping("/user/search/history/list")
    public ResultEntity getSearchHistory(@RequestParam(value = "userId", required = false) Long userId,
                                         @RequestParam(value = "browserId", required = false) String browserId) {
        List<SearchHistoryVO> resList = searchHistoryService.getSearchHistory(userId, browserId);
        return Results.success(resList);
    }

    /**
     * 删除单个查询记录
     *
     * @param searchHistoryId
     * @return
     */
    @PostMapping("/user/search/history/delete")
    public ResultEntity deleteSearchHistory(@RequestParam("searchHistoryId") Long searchHistoryId) {
        boolean res = searchHistoryService.deleteSearchHistory(searchHistoryId);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 添加查询记录
     *
     * @param searchHistoryTO
     * @return
     */
    @PostMapping("/user/search/history/add")
    public ResultEntity addSearchHistory(@RequestBody SearchHistoryTO searchHistoryTO) {
        boolean res = searchHistoryService.addSearchHistory(searchHistoryTO);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 清除查询记录
     *
     * @param userId
     * @param browserId
     * @return
     */
    @PostMapping("/user/search/history/clear")
    public ResultEntity clearSearchHistory(@RequestParam(value = "userId", required = false) Long userId,
                                           @RequestParam(value = "browserId", required = false) String browserId) {
        boolean res = searchHistoryService.clearSearchHistory(userId, browserId);
        if (!res) return Results.failure();
        return Results.success();
    }

}
